import jwt
from fastapi import HTTPException

from config import settings


def create_token(user: dict) -> str:
    return jwt.encode(
        payload=user,
        key=settings.token_secret,
        algorithm="HS256"
    )


def verify_token(token: str) -> dict:
    try:
        return jwt.decode(token, settings.token_secret, algorithms=["HS256"])
    except jwt.InvalidTokenError:
        raise HTTPException(status_code=401, detail="Invalid token")
